package com.simpleaccount.Search;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.daimajia.swipe.SwipeLayout;
import com.daimajia.swipe.adapters.BaseSwipeAdapter;
import com.simpleaccount.Cloud.CloudOperations;
import com.simpleaccount.Common.Utils;
import com.simpleaccount.Db.DbUtils;
import com.simpleaccount.Entity.Entry;
import com.simpleaccount.R;
import com.simpleaccount.modify.ModifyActivity;

import java.util.List;


/**
 * Created by Administrator on 2017/7/14.
 */
public class MyListViewAdapter extends BaseSwipeAdapter implements View.OnClickListener{
    private List<Entry> entries;
    private Context context;
    private SwipeLayout swipeLayout;
    private int clickPosition=-1;


    public MyListViewAdapter(List<Entry> entries,Context context){
        this.context=context;
        this.entries=entries;
    }
    @Override
    public int getSwipeLayoutResourceId(int position) {
        return R.id.sl_entry;
    }

    @Override
    public View generateView(int position, ViewGroup parent) {
        View view =LayoutInflater.from(context).inflate(R.layout.base_lv_entry,null);
        return view;
    }

    @Override
    public void fillValues(final int position, View convertView) {
        final Entry e = entries.get(position);
        final MyViewHolder holder = new MyViewHolder(convertView);

        swipeLayout =(SwipeLayout)convertView.findViewById(getSwipeLayoutResourceId(position));
        swipeLayout.close();
        swipeLayout.setShowMode(SwipeLayout.ShowMode.PullOut);
        swipeLayout.addDrag(SwipeLayout.DragEdge.Right,convertView.findViewById(R.id.ll_hidden_menu));
        swipeLayout.addSwipeListener(new SwipeLayout.SwipeListener() {
            @Override
            public void onStartOpen(SwipeLayout layout) {
                clickPosition=position;
            }

            @Override
            public void onOpen(SwipeLayout layout) {
                clickPosition=position;
                holder.getIvLvDropdown().setImageResource(R.drawable.arrow_right);
            }

            @Override
            public void onStartClose(SwipeLayout layout) {
                clickPosition=-1;
            }

            @Override
            public void onClose(SwipeLayout layout) {
                holder.getIvLvDropdown().setImageResource(R.drawable.arrow_left);
            }

            @Override
            public void onUpdate(SwipeLayout layout, int leftOffset, int topOffset) {

            }

            @Override
            public void onHandRelease(SwipeLayout layout, float xvel, float yvel) {

            }
        });

        Utils.setImage(holder.getIvCategory(),e.getCategory());
        holder.getTvLvType().setText(e.getType()==0?"支出":"收入");
        holder.getTvLvMoney().setText(String.format("%.2f",e.getMoney())+"元");
        holder.getTvLvCategory().setText(e.getCategory());
        holder.getIvHiddenDelete().setOnClickListener(this);
        holder.getIvHiddenModify().setOnClickListener(this);
        holder.getLlEntry().setBackgroundResource(R.drawable.shape_settings);

    }

    @Override
    public int getCount() {
        return entries.size();
    }

    @Override
    public Object getItem(int position) {

        return null;
    }

    @Override
    public long getItemId(int position) {
        return entries.get(position).getId();
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.iv_hidden_delete:
                int id=entries.get(clickPosition).getId();
                CloudOperations.singleDelete(id,context);
                DbUtils.deleteEntry(id);
                entries.remove(clickPosition);
                notifyDataSetChanged();
                break;
            case R.id.iv_hidden_modify:
                Entry entry=DbUtils.getEntryById(entries.get(clickPosition).getId());
                Double number=entry.getMoney();
                String category=entry.getCategory();
                int year=entry.getDate().getYear()+1900;
                int month=entry.getDate().getMonth();
                int day=entry.getDate().getDate();
                String remark=entry.getMsg();
                Intent mIntent = new Intent(context, ModifyActivity.class);
                Bundle mBundle = new Bundle();
                mBundle.putSerializable("entry", entry);
                mIntent.putExtras(mBundle);
                mIntent.setClass(context, ModifyActivity.class);
                ((SearchActivity)context).startActivityForResult(mIntent,0);
                break;
        }
    }
    public void setData(List<Entry> entries){

        this.entries=entries;
    }
}
